#!/usr/bin/env sh

COLOR=#EE82EE

INFO=$(wpctl status | awk '/^Audio/{in_audio=1} in_audio && /Sinks:/{in_sinks=1} in_sinks && /\*/{gsub(/.*\*/, ""); print $0; exit}')

# echo ${INFO} | grep 
read ID VOL < <(echo ${INFO} | sed -E 's/([0-9]+)\. .*([0-9]\.[0-9]+).*/\1 \2/g')

if [ -n "$(echo ${INFO} | grep MUTE)" ]; then
  ICON="\Uf0e08"
else
  ICON="\Uf028"
fi

if [ -n "$(wpctl inspect ${ID} |grep bluez)" ];then
  ICON="\Uf294${ICON}"
fi

VOL=$(echo "scale=0; ${VOL}*100/1" | bc)%

echo -e "${ICON} ${VOL}\n\n${COLOR}"

case $BLOCK_BUTTON in
    1) wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle ;;
    5) wpctl set-volume @DEFAULT_AUDIO_SINK@ 1%- ;;
    4) wpctl set-volume @DEFAULT_AUDIO_SINK@ 1%+  ;;
esac
